<template lang="pug">
  .page
    .AccountsReceivableManage
      .search-form.mb-10.pl-10.pr-10
        Form.ivu-row.ivu-form-basic(ref="formData" :model="formData" :rules="rules" :label-width="90")
          FormItem.form-item.ivu-col.pr-20.mb-15(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'select'")
              Select(v-if="item.model === 'insuranceName'" v-model="formData[item.model]" :placeholder="item.placeholder" clearable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.name"
                  :key="index"
                ) {{ c_item.name }}
              Select(v-else-if="item.model === 'serverName'" v-model="formData[item.model]" :placeholder="item.placeholder" clearable filterable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.label"
                  :key="index"
                ) {{ c_item.label }}
            template(v-if="item.type === 'input'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder" clearable)
            template(v-if="item.type === 'radio'")
              RadioGroup(type="button" v-model="formData[item.model]")
                Radio.display-inline-block(v-for="c_item of item.radioArr" :label="c_item.value") {{c_item.label}}
            template(v-if="item.type === 'intervalMoney'")
              Input.pull-left.w100(v-model="formData[item.model1]" :placeholder="item.placeholder1" clearable)
              span.pl-10.pr-10.pull-left.line-h-32 --
              Input.pull-left.w100(v-model="formData[item.model2]" :placeholder="item.placeholder2" clearable)
            template(v-if="item.type === 'intervalM'")
              DatePicker.pull-left.interval(v-model="formData[item.model1]" :placeholder="item.placeholder1" type="month" @on-change="(val, type) => handleChangeDatePicker(val, 'month', item.model1)")
              span.pl-10.pr-10.pull-left.line-h-32 --
              DatePicker.pull-left.interval(v-model="formData[item.model2]" :placeholder="item.placeholder2" type="month" @on-change="(val, type) => handleChangeDatePicker(val, 'month', item.model2)")
            template(v-if="item.type === 'intervalD'")
              DatePicker.pull-left.interval-date(v-model="formData[item.model1]" :placeholder="item.placeholder1" type="date" @on-change="(val, type) => handleChangeDatePicker(val, 'date', item.model1)")
              span.pl-10.pr-10.pull-left.line-h-32 --
              DatePicker.pull-left.interval-date(v-model="formData[item.model2]" :placeholder="item.placeholder2" type="date" @on-change="(val, type) => handleChangeDatePicker(val, 'date', item.model2)")
            template(v-if="item.type === 'button'")
              Button(type="info" @click="searchFn") 查询

      .pl-20.pr-20.bg-white
        Table.hasTitle-table.beautiful-table(:columns="thead" :data="tableData" :stripe="true" ref="demoTable"
          @on-selection-change="handleOnSelectChange"
        )
          .clearfix.overflow-hidden(slot="header")
            .table-title-lefttxt.pull-left
              |合计
              span.text-info.pl-5.pr-5.font-weight {{tableComone.totalRows}}
              |条
              Divider(type="vertical")
              |合计
              span.text-info.pl-5.pr-5.font-weight {{tableComone.totalMoney}}
              |元
            .pull-right.table-title-btns
              Button.mr-10(type="info" @click="exportBankReceivableBill") 导出
              Button(type="info" @click="exportCollectionDocReceived") 导出收款单-U8
          .clearfix.pl-20.pr-20(slot="footer")
            Page(
              :total="tableComone.totalRows"
              @on-change="onChangePage"
              @on-page-size-change="onpageSizeChange"
              show-sizer
              show-total
              placement="top"
              prev-text="上一页"
              next-text="下一页"
              :page-size-opts="[10, 20, 30, 50, 100]"
            )
</template>

<script>
import { Common } from '@/utils/common.js'
import { ReceivableBillApi } from '@/api/ReceivableBill.api.js'
import { EnumApi } from '@/api/Enum.api.js'
import { BankCashArrivalThead } from './BCtableHead.js'
import _cloneDeep from 'clone-deep'
import { EmployeesApi } from '@/api/Employees.api.js'
import config from '@/config'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro.replace('/api', '/')

export default {
  name: 'bankCashArrivalManagementNew', // 银行到款管理页面

  components: {
  },

  data () {
    return {
      companyId: '', // 公司ID
      companyName: '', // 公司名称
      employeeId: '', // 员工ID
      thead: _cloneDeep(BankCashArrivalThead),
      tableData: [],
      formData: {
        companyName: '',
        bankName: '',
        FeeYearMonthStart: this.$moment(new Date()).format('YYYY-MM'),
        FeeYearMonthEnd: this.$moment(new Date()).format('YYYY-MM'),
        ReceiveDateStart: '',
        ReceiveDateEnd: '',
        TotalStart: '',
        TotalEnd: '',
        // securityNo: '',
        // serverName: ''
      },
      formItem: [
        { type: 'input', label: '客户名称：', placeholder: '请输入客户名称', prop: 'companyName', model: 'companyName', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-12 ivu-col-span-xxl-8' },
        { type: 'input', label: '到款银行：', placeholder: '请输入到款银行', prop: 'bankName', model: 'bankName', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-12 ivu-col-span-xxl-8' },
        // {type: 'select', label: '到款项目：', placeholder: '请选择到款项目', prop: 'insuranceName', model: 'insuranceName', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-12 ivu-col-span-xxl-8', options: 'programNames'},
        { type: 'intervalM', label: '费用月份：', placeholder1: '开始日期', prop1: 'FeeYearMonthStart', model1: 'FeeYearMonthStart', placeholder2: '结束日期', prop2: 'FeeYearMonthEnd', model2: 'FeeYearMonthEnd', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-12 ivu-col-span-xxl-8' },
        { type: 'intervalD', label: '到款日期：', placeholder1: '开始日期', prop1: 'ReceiveDateStart', model1: 'ReceiveDateStart', placeholder2: '结束日期', prop2: 'ReceiveDateEnd', model2: 'ReceiveDateEnd', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-12 ivu-col-span-xxl-8' },
        { type: 'intervalMoney', label: '到款金额：', placeholder1: '起始金额', prop1: 'TotalStart', model1: 'TotalStart', placeholder2: '结束金额', prop2: 'TotalEnd', model2: 'TotalEnd', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-12 ivu-col-span-xxl-8' },
        // {type: 'input', label: '参保编号：', placeholder: '请输入参保编号', prop: 'securityNo', model: 'securityNo', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-12 ivu-col-span-xxl-8'},
        // {type: 'select', label: '所属客服：', placeholder: '请选择所属客服', prop: 'serverName', model: 'serverName', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-12 ivu-col-span-xxl-8', options: 'server'},
        { type: 'button', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-4 ivu-col-span-xxl-4' }
      ],
      rules: {
        IdCardNo: [
          { type: 'string', message: '请输入正确的身份证号码', pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/ }
        ]
      },
      selectedEmployeeId: [], // 勾选的id集合,array格式
      selectedEmployeeArr: [], // 勾选的id, name数组集合
      tableComone: {
        pageIndex: 1, // 页码
        totalRows: 0, // 总条数
        pageSize: 10, // 当前页面展示条数
        totalMoney: 0 // 合计金额
      },
      downLoadRul: '',
      arrListDown: {
        server: [],
        programNames: []
      },
      datePickerDefault: new Date(), // 当前时间
      importExcel: {
        uploadLoading: false,
        progressPercent: 0,
        showProgress: false,
        showRemoveFile: false,
        file: null
      },
      nowData: this.$moment(new Date()).format('YYYY-MM')
    }
  },

  mounted () {
    this.query()
    // this.getReceiveProgramNames()
    // this.servers()
  },

  methods: {
    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1
      this.query()
    },

    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await ReceivableBillApi.bankReceivableBill({ ...params, ...this.formData })
      console.log('银行到款res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
        this.tableComone.totalMoney = res.data.totalMoney
      } else {
        this.tableData = []
      }
      this.loading = false
    },

    // // 获取到款项目
    // async getReceiveProgramNames () {
    //   var params = {
    //     PageIndex: 1,
    //     PageSize: 100,
    //     Args: ''
    //   }
    //   let res = await ReceivableBillApi.getReceiveProgramNames(params)
    //   console.log('获取到款项目res: ', res)
    //   if (res.status === 200) {
    //     this.arrListDown.programNames = res.data.data
    //   }
    // },

    // // 获取客服
    // async servers () {
    //   var params = {
    //     PageIndex: 1,
    //     PageSize: 100,
    //     Args: ''
    //   }
    //   Common.servers(params, this.arrListDown, 'server')
    // },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedEmployeeArr = value.map((item) => {
        return { id: item.id, name: item.name }
      })
      this.selectedEmployeeId = value.map((item) => {
        return item.id
      })
      console.log('列表勾选数组1', this.selectedEmployeeArr)
      console.log('列表勾选数组2', this.selectedEmployeeId)
    },

    // 日期选择
    handleChangeDatePicker (val, type, model) {
      console.log('val: ', val)
      if (val) {
        if (type === 'month') {
          this.formData[model] = this.$moment(val).format('YYYY-MM')
        } else {
          this.formData[model] = this.$moment(val).format('YYYY-MM-DD')
        }
      } else {
        this.formData[model] = ''
      }
    },

    // 导出员工数据
    async exportEmployees () {
      console.log('formInvoice: ', this.formInvoice)
      let res = await EmployeesApi.exportEmployees(this.formInvoice)
      console.log('res: ', res)
      if (res.status === 200 && res.data.success) {
        console.log('❤ 返回链接: ', baseUrl + res.data.message)
        window.open(baseUrl + res.data.message, '_blank')
      }
    },

    // 到款填写
    goReceivePaymentCreate (companyId, companyCode, companyName, periodYear, periodMonth) {
      this.$router.push({
        path: '/ReceivePaymentCreate',
        name: 'ReceivePaymentCreate',
        query: {
          companyId: companyId,
          companyName: companyName,
          companyCode: companyCode,
          year: periodYear,
          month: periodMonth
        }
      })
    },

    // 导出银行到款
    async exportBankReceivableBill () {
      let res = await ReceivableBillApi.exportBankReceivableBill(this.formData)
      if (res.status === 200 && res.data.success) {
        window.open(baseUrl + res.data.message, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 导出收款单
    async exportCollectionDocReceived () {
      let res = await ReceivableBillApi.exportCollectionDocReceived()
      if (res.status === 200 && res.data.success) {
        window.open(baseUrl + res.data.message, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    }
  } // methods end
}
</script>

<style lang="less" scoped>

</style>
